//
//  AppDelegate.swift
//  XMWeimo
//
//  Created by ios001 on 16/7/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SVProgressHUD
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//        XMSQLManager.sharedTools
//        let result = XMStatusDAL.getCache(0, maxId: 0)
//        print(result)
        setupAppStyle()
        
        // 初始化window
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        // 设置背景颜色
        self.window?.backgroundColor = UIColor.whiteColor()
        // 设置根控制器
        setupDefaultRootVc()
//        self.window?.rootViewController = UINavigationController(rootViewController: XMComposeViewController())
        // 设置成为主窗口并可见
        self.window?.makeKeyAndVisible()
        
        // 接收通知切换控制器
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootVc:", name: WBSwitchRootViewControllerNoti, object: nil)
        isNewVersion()
        return true
    }
    // MARK: - 设置默认的根控制器
    // 如果用户已经登录
     // 如果是最新版本
    // rootVc = NewFeature
    // 如果不是最新版本
    // rootVc = WelcomeVc
    // 如果用户没有登录
    // rootVc = MainVc
    
    func setupDefaultRootVc(){
        // - 如果用户已经登录 rootVc->welcome
        // - 如果用户没有登录 rootVc->mainVc
        if XMUserAccountViewModel.sharedTools.isLogin {
            // 如果是最新版
            if isNewVersion(){
                self.window?.rootViewController = XMNewfeatureViewController()
            }else {
                // 如果不是最新版
                self.window?.rootViewController = XMWelcomeViewController()
            }
        }else {
            self.window?.rootViewController = XMMainViewController()
        }
    }
    
    // 切换控制器
    func switchRootVc(noti: NSNotification){
//        print(noti.object)
        // 代表 OAuthVc ->welcomeVc
        if noti.object == nil {
            self.window?.rootViewController = XMWelcomeViewController()
        }else {
            // 代表 OAuthVc ->MainVc
            // 由于object 不为nil 切换控制器均为MainVc
            self.window?.rootViewController = XMMainViewController()
        }
    }
    
    // app相关设置
    func setupAppStyle(){
        // 设置tintColor 设置空间颜色(全局使用,必须在项目实例化之前设置)
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        SVProgressHUD.setDefaultStyle(SVProgressHUDStyle.Custom)
        SVProgressHUD.setBackgroundColor(ThemeColor)
        SVProgressHUD.setForegroundColor(UIColor.whiteColor())
    }
    
    // 判断当前是否是最新版本
    func isNewVersion() -> Bool {
        // 获取当前的版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        // 把字符串转成double类型
        let currentVersionD = Double(currentVersion)!
        // 获取通过偏好设置保存的版本
        let xmAppVersion = "xmAppVersion"
        let oldVersion = NSUserDefaults.standardUserDefaults().doubleForKey(xmAppVersion)
        // 保存当前获取到的版本号
        NSUserDefaults.standardUserDefaults().setDouble(currentVersionD, forKey: xmAppVersion)
//        print(currentVersionD)
//        print(oldVersion)
        // 返回是否是最新版本
        return currentVersionD > oldVersion
    }
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    // 程序进入后台或者被杀
    func applicationDidEnterBackground(application: UIApplication) {
        XMStatusDAL.deleteCache()
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

